/**
 * 功能描述:
 * 相关配置文件:
 * @author Administrator
 */
package cn.framework.mail.handle;

import javax.jms.JMSException;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import cn.framework.mail.SysMailSender;
import cn.framework.mail.model.Mail;


@Service
public class handleMail {
	
	@Autowired
	SysMailSender mailSender;
	
	@Autowired
	@Qualifier("sysMailSubject")
	String sysMailSubject;
	
	@Autowired
	@Qualifier("sysMailFrom")
	String sysMailFrom;
	
	@Autowired
	@Qualifier("sysMailDisplayName")
	String sysMailDisplayName;
	
	public Mail initMail(Mail sysMail){
		if(StringUtils.isBlank(sysMail.getSubject())){
			sysMail.setSubject(sysMailSubject);
		}
		if(StringUtils.isBlank(sysMail.getForm())){
			sysMail.setForm(sysMailFrom);
		}
		if(StringUtils.isBlank(sysMail.getDisplayName())){
			sysMail.setDisplayName(sysMailDisplayName);
		}
		return sysMail;
	}

	public void handleMessage(Mail sysMail) throws JMSException {
		sysMail=initMail(sysMail);
		switch (sysMail.getType()) {
		case Mail.MAIL_HTML_TYPE:
			sendHtmlMail(sysMail);
			break;
		default:
			sendMail(sysMail);
			break;
		}
	}
	public void sendHtmlMail(Mail sysMail){
		mailSender.sendHTMLMail(sysMail.getTargetMailList(), sysMail.getSubject(), sysMail.getTemplateName(), sysMail.getModel());
	}
	public void sendMail(Mail sysMail){
		mailSender.sendMail(sysMail.getTargetMailList(), sysMail.getSubject(), sysMail.getContent());
	}
}
